package com.dc.gameserver.serverCore.service.effective.l1;

import com.dc.gameserver.baseConfig.Config;
import com.dc.gameserver.exceptions.logicException;
import com.dc.gameserver.proto.itemserverprotoVo.attrVoResponse;
import com.dc.gameserver.serverCore.model.dbsql.modelCache.ModelCacheService;
import com.dc.gameserver.serverCore.model.dbsql.modelService.PlayerService;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TRole;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TRoleBook;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TRoleEqu;
import com.dc.gameserver.serverCore.model.gamemodel.BookModel;
import com.dc.gameserver.serverCore.model.gamemodel.EquipmentModel;
import com.dc.gameserver.serverCore.model.gamemodel.Model;
import com.dc.gameserver.serverCore.service.battle.GameFormula;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import com.dc.gameserver.serverCore.service.effective.abstractEffective;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 石头哥哥 </br>
 *         dcserver1.3 </br>
 *         Date:13-12-30 </br>
 *         Time:下午5:37 </br>
 *         Package:{@link com.dc.gameserver.serverCore.service.effective.l1}</br>
 *         Comment：  穿戴增加气血值上限
 */
@Service
@SuppressWarnings("unchecked")
public class _103_Effective extends abstractEffective {

    @Resource
    private ModelCacheService modelCacheService;

    @Resource
    private PlayerService playerService;
    /**
     * spring 初始化索引
     */
    @Override
    public void PostConstruct() {
      EFFECTIVE_ID[0x67]=this;
    }


    /**
     * @param player
     * @param model   book ,equ
     * @param goodsID itemID 装备  秘籍编号
     * @param flag    1.number 2.percent
     */
    @Override
    public void effect(PlayerInstance player, Model model, int goodsID, int flag,boolean add,attrVoResponse.Builder builder) {
        int roleID=player.getID();
        String role_prefix=ModelCacheService.ROLE_PREFIX(roleID);
        TRole tRole= (TRole) modelCacheService.findByPrefixID(role_prefix);
        if (model instanceof EquipmentModel){
            List<TRoleEqu> equList= (List<TRoleEqu>) modelCacheService.findListByPrefixID(ModelCacheService.EQU_PREFIX(roleID));
            TRoleEqu equ=null;
            for (TRoleEqu roleEqu:equList){
                if (roleEqu.getEquid()==goodsID){
                    equ=roleEqu;
                    break;
                }
            }
            if (equ==null){
                throw new logicException("装备的对象缓存不存在... ...,检查逻辑");
            }
            EquipmentModel equipmentModel= (EquipmentModel) model;
            if (flag== 0x1){
                Double num=equipmentModel.getEffectNum1();
                if (num!= 0x0){
                    double attactPower=equipmentModel.getEffectNumGrowUp1();
                    int equAttr=(int) GameFormula.goodsAttr(num, equ.getEqulevel(), attactPower, equ.getEqupinjie(),
                            equipmentModel.getSubQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinJieModulus
                            , equ.getEqupinzhi(), equipmentModel.getQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinZhiModulus);
                    if (add&&builder!=null){
                        tRole.setRolemaxblood(tRole.getRolemaxblood()+equAttr);
                        builder.setRoleBlood(tRole.getRolemaxblood());
                    }else {
                        tRole.setRolemaxblood(tRole.getRolemaxblood()-equAttr);
                    }
                }
                num=equipmentModel.getEffectNum2();
                if (num!=0x0){
                    double attactPower=equipmentModel.getEffectNumGrowUp2();
                    int equAttr=(int) GameFormula.goodsAttr(num, equ.getEqulevel(), attactPower, equ.getEqupinjie(),
                            equipmentModel.getSubQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinJieModulus
                            , equ.getEqupinzhi(), equipmentModel.getQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinZhiModulus);
                    if (add&&builder!=null){
                        tRole.setRolemaxblood(tRole.getRolemaxblood()+equAttr);
                        builder.setRoleBlood(tRole.getRolemaxblood());
                    }else {
                        tRole.setRolemaxblood(tRole.getRolemaxblood()-equAttr);
                    }
                }
                num=equipmentModel.getEffectNum3();
                if (num!=0x0){
                    double attactPower=equipmentModel.getEffectNumGrowUp3();
                    int equAttr=(int) GameFormula.goodsAttr(num, equ.getEqulevel(), attactPower, equ.getEqupinjie(),
                            equipmentModel.getSubQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinJieModulus
                            , equ.getEqupinzhi(), equipmentModel.getQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinZhiModulus);
                    if (add&&builder!=null){
                        tRole.setRolemaxblood(tRole.getRolemaxblood()+equAttr);
                        builder.setRoleBlood(tRole.getRolemaxblood());
                    }else {
                        tRole.setRolemaxblood(tRole.getRolemaxblood()-equAttr);
                    }
                }
            }else {
                Double percent=equipmentModel.getEffectPercent1();
                if (percent!= 0x0){
                    double attactPower=equipmentModel.getEffectPercent1();
                    int equAttr=(int) GameFormula.goodsAttr(percent, equ.getEqulevel(), attactPower, equ.getEqupinjie(),
                            equipmentModel.getSubQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinJieModulus
                            , equ.getEqupinzhi(), equipmentModel.getQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinZhiModulus);
                    if (add&&builder!=null){
                        tRole.setRolemaxblood(tRole.getRolemaxblood()+equAttr);
                        builder.setRoleBlood(tRole.getRolemaxblood());
                    }else {
                        tRole.setRolemaxblood(tRole.getRolemaxblood()-equAttr);
                    }
                }
                percent=equipmentModel.getEffectPercent2();
                if (percent!= 0x0){
                    double attactPower=equipmentModel.getEffectPercent2();
                    int equAttr=(int) GameFormula.goodsAttr(percent, equ.getEqulevel(), attactPower, equ.getEqupinjie(),
                            equipmentModel.getSubQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinJieModulus
                            , equ.getEqupinzhi(), equipmentModel.getQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinZhiModulus);
                    if (add&&builder!=null){
                        tRole.setRolemaxblood(tRole.getRolemaxblood()+equAttr);
                        builder.setRoleBlood(tRole.getRolemaxblood());
                    }else {
                        tRole.setRolemaxblood(tRole.getRolemaxblood()-equAttr);
                    }
                }
                percent=equipmentModel.getEffectPercent3();
                if (percent!= 0x0){
                    double attactPower=equipmentModel.getEffectPercent3();
                    int equAttr=(int) GameFormula.goodsAttr(percent, equ.getEqulevel(), attactPower, equ.getEqupinjie(),
                            equipmentModel.getSubQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinJieModulus
                            , equ.getEqupinzhi(), equipmentModel.getQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinZhiModulus);
                    if (add&&builder!=null){
                        tRole.setRolemaxblood(tRole.getRolemaxblood()+equAttr);
                        builder.setRoleBlood(tRole.getRolemaxblood());
                    }else {
                        tRole.setRolemaxblood(tRole.getRolemaxblood()-equAttr);
                    }
                }
            }
            playerService.updateRole(tRole);
            modelCacheService.addObjectPrefixID(role_prefix,tRole);
            return;
        }

        //秘籍
        if (model instanceof BookModel){
            BookModel bookModel= (BookModel) model;
            List<TRoleBook> bookList= (List<TRoleBook>) modelCacheService.findListByPrefixID(ModelCacheService.BOOK_PREFIX(roleID));
            TRoleBook book= null;
            for (TRoleBook tRoleBook:bookList){
                if (tRoleBook.getBookid()==goodsID){
                    book=tRoleBook;
                    break;
                }
            }
            if (book==null){
                throw new logicException("秘籍的对象缓存不存在... ...,检查逻辑");
            }
            if (flag== 0x1){  //
                Double num=bookModel.getEffectNum1();
                if (num!= 0x0){
                    double attack=bookModel.getEffectNumGrowUp1();
                    int bookAttr=(int) GameFormula.goodsAttr(num,book.getBooklevel(),attack,book.getBookpinjie(),
                            bookModel.getSubQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinJieModulus
                            ,book.getBookpinzhi(),bookModel.getQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinZhiModulus);
                    if (add&&builder!=null){
                        tRole.setRolemaxblood(tRole.getRolemaxblood()+bookAttr);
                        builder.setRoleBlood(tRole.getRolemaxblood());
                    }else {
                        tRole.setRolemaxblood(tRole.getRolemaxblood()-bookAttr);
                    }
                }
                num=bookModel.getEffectNum2();
                if (num!=0x0){
                    double attack=bookModel.getEffectNumGrowUp2();
                    int bookAttr=(int) GameFormula.goodsAttr(num,book.getBooklevel(),attack,book.getBookpinjie(),
                            bookModel.getSubQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinJieModulus
                            ,book.getBookpinzhi(),bookModel.getQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinZhiModulus);
                    if (add&&builder!=null){
                        tRole.setRolemaxblood(tRole.getRolemaxblood()+bookAttr);
                        builder.setRoleBlood(tRole.getRolemaxblood());
                    }else {
                        tRole.setRolemaxblood(tRole.getRolemaxblood()-bookAttr);
                    }
                }
                num=bookModel.getEffectNum3();
                if (num!=0x0){
                    double attack=bookModel.getEffectNumGrowUp3();
                    int bookAttr=(int) GameFormula.goodsAttr(num,book.getBooklevel(),attack,book.getBookpinjie(),
                            bookModel.getSubQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinJieModulus
                            ,book.getBookpinzhi(),bookModel.getQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinZhiModulus);
                    if (add&&builder!=null){
                        tRole.setRolemaxblood(tRole.getRolemaxblood()+bookAttr);
                        builder.setRoleBlood(tRole.getRolemaxblood());
                    }else {
                        tRole.setRolemaxblood(tRole.getRolemaxblood()-bookAttr);
                    }
                }
            }else {
                Double percent=bookModel.getEffectPercent1();
                if (percent!= 0x0){
                    double attack=bookModel.getEffectPercentGrowUp1();
                    int bookAttr=(int) GameFormula.goodsAttr(percent,book.getBooklevel(),attack,book.getBookpinjie(),
                            bookModel.getSubQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinJieModulus
                            ,book.getBookpinzhi(),bookModel.getQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinZhiModulus);
                    if (add&&builder!=null){
                        tRole.setRolemaxblood(tRole.getRolemaxblood()+bookAttr);
                        builder.setRoleBlood(tRole.getRolemaxblood());
                    }else {
                        tRole.setRolemaxblood(tRole.getRolemaxblood()-bookAttr);
                    }
                }
                percent=bookModel.getEffectPercent2();
                if (percent!= 0x0){
                    double attack=bookModel.getEffectPercentGrowUp2();
                    int bookAttr=(int) GameFormula.goodsAttr(percent,book.getBooklevel(),attack,book.getBookpinjie(),
                            bookModel.getSubQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinJieModulus
                            ,book.getBookpinzhi(),bookModel.getQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinZhiModulus);
                    if (add&&builder!=null){
                        tRole.setRolemaxblood(tRole.getRolemaxblood()+bookAttr);
                        builder.setRoleBlood(tRole.getRolemaxblood());
                    }else {
                        tRole.setRolemaxblood(tRole.getRolemaxblood()-bookAttr);
                    }
                }
                percent=bookModel.getEffectPercent3();
                if (percent!= 0x0){
                    double attack=bookModel.getEffectPercentGrowUp3();
                    int bookAttr=(int) GameFormula.goodsAttr(percent,book.getBooklevel(),attack,book.getBookpinjie(),
                            bookModel.getSubQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinJieModulus
                            ,book.getBookpinzhi(),bookModel.getQuality(), Config.DEFAULT_VALUE.GAME_VALUE.PinZhiModulus);
                    if (add&&builder!=null){
                        tRole.setRolemaxblood(tRole.getRolemaxblood()+bookAttr);
                        builder.setRoleBlood(tRole.getRolemaxblood());
                    }else {
                        tRole.setRolemaxblood(tRole.getRolemaxblood()-bookAttr);
                    }
                }
            }
            playerService.updateRole(tRole);
            modelCacheService.addObjectPrefixID(role_prefix,tRole);
        }
    }
}
